package com.mos.iot.mqtt.listener;

import cn.hutool.core.text.AntPathMatcher;
import cn.hutool.core.util.StrUtil;
import com.mos.iot.mqtt.constants.MqttConstants;
import reactor.core.publisher.Mono;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class MqttTopicListenerManager {

    private final static Map<String, Map<String, List<Map<Method, Object>>>> listenerMap = new ConcurrentHashMap<>();

    /**
     * 添加主题消息监听
     * @param clientId 客户端id
     * @param topic 主题
     * @param method 回调方法
     */
    public static void addListener(String clientId, String topic, Object bean, Method method) {
        Map<String, List<Map<Method, Object>>> clientSubscribeTopics = listenerMap.computeIfAbsent(StrUtil.blankToDefault(clientId, MqttConstants.DEFAULT_CLIENT_ID),k -> new ConcurrentHashMap<>());
        List<Map<Method, Object>> topicMethods = clientSubscribeTopics.computeIfAbsent(topic, k -> new ArrayList<>());
        Map<Method, Object> map = new HashMap<>();
        map.put(method, bean);
        topicMethods.add(map);
    }

    /**
     * 获取主题消息监听
     * @param clientId 客户端id
     */
    public static Map<String, List<Map<Method, Object>>> getListener(String clientId) {
        return listenerMap.get(clientId);
    }

    public static void main(String[] args) {
        //Mono<String> mono = Mono.just("Hello World!").contextWrite(context -> context.put("key", "value"));
        //mono
    }
}
